package com.wenchao.quickstart.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * Toast提示工具<br>
 * <p>
 * 避免重复提示<br>
 * <p>
 * 比较规则：是否来自同一上下文
 */
public class ToastUtil {

    private static Context context;
    private static Toast LongToast;
    private static Toast ShortToast;

    /**
     * Long Toast
     *
     * @param context
     * @param content
     */
    public static void showLongToast(Context context, String content) {

        if (ToastUtil.context != null
                && ToastUtil.context.hashCode() == context.hashCode()) {
            // 当前已经有上下文对象,并且与新的显示上下文对象相同
            if (ToastUtil.LongToast == null) {
                ToastUtil.LongToast = Toast.makeText(context,
                        "Base Long Toast", Toast.LENGTH_LONG);
            }
        } else {
            ToastUtil.context = context;
            ToastUtil.LongToast = Toast.makeText(context, "Base Long Toast",
                    Toast.LENGTH_LONG);
        }

        ToastUtil.LongToast.setText(content);
        ToastUtil.LongToast.show();

    }

    public static void showLongToast(Context context, String content, int gravity) {
        if (ToastUtil.context != null
                && ToastUtil.context.hashCode() == context.hashCode()) {
            // 当前已经有上下文对象,并且与新的显示上下文对象相同
            if (ToastUtil.LongToast == null) {
                ToastUtil.LongToast = Toast.makeText(context, "Base Long Toast", Toast.LENGTH_LONG);
            }
        } else {
            ToastUtil.context = context;
            ToastUtil.LongToast = Toast.makeText(context, "Base Long Toast", Toast.LENGTH_LONG);
        }
        ToastUtil.LongToast.setText(content);
        ToastUtil.LongToast.setGravity(gravity, 0, 0);
        ToastUtil.LongToast.show();
    }

    /**
     * Short Toast
     *
     * @param context
     * @param content
     */
    public static void showShortToast(Context context, String content) {

        if (ToastUtil.context != null
                && ToastUtil.context.hashCode() == context.hashCode()) {
            // 当前已经有上下文对象,并且与新的显示上下文对象相同
            if (ToastUtil.ShortToast == null) {
                ToastUtil.ShortToast = Toast.makeText(context,
                        "Base Short Toast", Toast.LENGTH_SHORT);
            }
        } else {
            ToastUtil.context = context;
            ToastUtil.ShortToast = Toast.makeText(context, "Base Short Toast",
                    Toast.LENGTH_SHORT);
        }

        ToastUtil.ShortToast.setText(content);
        ToastUtil.ShortToast.show();

    }

    public static void closeToast() {
        if (ToastUtil.ShortToast != null) {
            ToastUtil.ShortToast.cancel();
        }
    }


    /**
     * Long Toast 在屏幕中间显示toast
     *
     * @param context
     * @param content
     */
    public static void showLongToastCenter(Context context, String content) {

        if (ToastUtil.context != null
                && ToastUtil.context.hashCode() == context.hashCode()) {
            // 当前已经有上下文对象,并且与新的显示上下文对象相同
            if (ToastUtil.LongToast == null) {
                ToastUtil.LongToast = Toast.makeText(context,
                        "Base Long Toast", Toast.LENGTH_LONG);
            }
        } else {
            ToastUtil.context = context;
            ToastUtil.LongToast = Toast.makeText(context, "Base Long Toast",
                    Toast.LENGTH_LONG);
        }

        ToastUtil.LongToast.setGravity(Gravity.CENTER, 0, 0);

        ToastUtil.LongToast.setText(content);
        ToastUtil.LongToast.show();

    }

    /**
     * Short Toast
     *
     * @param context
     * @param content
     */
    public static void showShortToastCenter(Context context, String content) {

        if (ToastUtil.context != null
                && ToastUtil.context.hashCode() == context.hashCode()) {
            // 当前已经有上下文对象,并且与新的显示上下文对象相同
            if (ToastUtil.ShortToast == null) {
                ToastUtil.ShortToast = Toast.makeText(context,
                        "Base Short Toast", Toast.LENGTH_SHORT);
            }
        } else {
            ToastUtil.context = context;
            ToastUtil.ShortToast = Toast.makeText(context, "Base Short Toast",
                    Toast.LENGTH_SHORT);
        }

        ToastUtil.ShortToast.setGravity(Gravity.CENTER, 0, 0);

        ToastUtil.ShortToast.setText(content);
        ToastUtil.ShortToast.show();
    }

}